package com.xuerhua.yxt.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.xuerhua.yxt.common.enums.RelationType;
import com.xuerhua.yxt.dao.AttachmentListMapper;
import com.xuerhua.yxt.dao.StudentWeekCommentMapper;
import com.xuerhua.yxt.model.AttachmentList;
import com.xuerhua.yxt.model.StudentWeekComment;
import com.xuerhua.yxt.page.Page;
import com.xuerhua.yxt.reqVo.AttachRelationReqVO;
import com.xuerhua.yxt.reqVo.StudentWeekCommentReqVO;
import com.xuerhua.yxt.reqVo.WeekCommentQueryVO;
import com.xuerhua.yxt.service.IStudentWeekCommentService;
import com.xuerhua.yxt.utils.BeanUtils;
import com.xuerhua.yxt.view.StudentWeekCommentView;

@Service("studentWeekCommentService")
public class StudentWeekCommentServiceImpl implements IStudentWeekCommentService {

	@Autowired
	private StudentWeekCommentMapper studentWeekCommentMapper;
	@Autowired
	private AttachmentListMapper attachmentListMapper;
	
	@Override
	public int insertSelective(StudentWeekComment record) {
		return studentWeekCommentMapper.insertSelective(record);
	}

	@Override
	public StudentWeekComment selectByPrimaryKey(Integer id) {
		return studentWeekCommentMapper.selectByPrimaryKey(id);
	}

	@Override
	public int updateByPrimaryKeySelective(StudentWeekComment record) {
		return studentWeekCommentMapper.updateByPrimaryKeySelective(record);
	}

	@Override
	public void insert(StudentWeekCommentReqVO reqVO) {
		if(reqVO.getAttachIds() != null){
			reqVO.setIsAttach(1);
		}
		studentWeekCommentMapper.insertSelective(reqVO);
		if(reqVO.getAttachIds() != null) {
			AttachRelationReqVO attach = new AttachRelationReqVO();
			attach.setRelationId(Long.valueOf(reqVO.getId()));
			attach.setAttachIds(reqVO.getAttachIds());
			attachmentListMapper.updateRelationIdByIds(attach);
		}
	}
	
	@Override
	public List<StudentWeekCommentView> selectWeekCommentByStudentId(WeekCommentQueryVO vo) {
		return studentWeekCommentMapper.selectWeekCommentByStudentId(vo);
	}

	@Override
	public List<StudentWeekComment> selectWeekCommentListByPage(Page<StudentWeekComment> page) {
		return studentWeekCommentMapper.selectWeekCommentListByPage(page);
	}

	@Override
	public List<StudentWeekComment> selectWeekCommentListByPageForParent(Page<StudentWeekComment> page) {
		return studentWeekCommentMapper.selectWeekCommentListByPageForParent(page);
	}

	@Override
	public List<StudentWeekCommentView> selectWeekCommentListForStudent(WeekCommentQueryVO vo) {
		return studentWeekCommentMapper.selectWeekCommentListForStudent(vo);
	}

	@Override
	public Integer selectStatus(WeekCommentQueryVO vo) {
		return studentWeekCommentMapper.selectStatus(vo);
	}

	@Override
	public Integer selectWeekNo(WeekCommentQueryVO vo) {
		return studentWeekCommentMapper.selectWeekNo(vo);
	}

	@Override
	public List<StudentWeekCommentView> selectViewList(WeekCommentQueryVO vo) {
		return studentWeekCommentMapper.selectViewList(vo);
	}

	@Override
	public List<StudentWeekCommentView> selectRemindTeacher(WeekCommentQueryVO vo) {
		return studentWeekCommentMapper.selectRemindTeacher(vo);
	}

	@Override
	public StudentWeekCommentView selectViewById(StudentWeekComment swc) {
		StudentWeekComment comment = studentWeekCommentMapper.selectById(swc);
		StudentWeekCommentView view = BeanUtils.copyAs(comment, StudentWeekCommentView.class);
		if(comment.getIsAttach() == 1) {
			Map<String, Object> map = new HashMap<>();
			map.put("type", RelationType.WEEK_COMMENT.getCode());
			map.put("objId", swc.getId());
			map.put("schoolId",swc.getSchoolId());
			List<AttachmentList> attachmentList = attachmentListMapper.queryAttachmentListByRelationId(map);
			view.setAttachList(attachmentList);
		}
		return view;
	}

	@Override
	public List<StudentWeekCommentView> selectStudentCommentList(WeekCommentQueryVO vo) {
		return studentWeekCommentMapper.selectStudentCommentList(vo);
	}

}
